package com.example.bootdemo.utils;

import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierDemo {

    	private static final String[] arr = {"小王","小张","小李","小赵","小丽","小陈","小孙","小郭","小吴","小钱"};
   // private static String[] arr = {"小王","小张","小李","小赵","小丽"};

    public static void main(String[] args)throws Exception{
        changjing();
    }

    public static void changjing()throws Exception{
        CyclicBarrier cyclicBarrier = new CyclicBarrier(5,new Runnable() {
            @Override
            public void run() {
                System.out.println("5人已来,可以开席了");
            }
        });
        for (int i = 0; i < 10; i++) {
            final int index = i;
            Thread.sleep(1000);
            new Thread(()->{
                try {
                    System.out.println(arr[index] + "要来了");
                    cyclicBarrier.await();
                    System.out.println(arr[index] + "开始吃了");
                } catch (Exception e) {

                }
            },"线程" + i).start();
        }
    }
}
